#! /usr/bin/python
# ! -*- coding:UTF-8 -*-

import Tkinter as tk
import tkFileDialog

from iButton import *


class LFrame():
    _root = None
    _all_btns = []  # 传入的存储所有按钮的button
    _selected_btns = []  # 传入的存储所有已选择按钮的button
    _dp = None

    def __init__(self, dp):
        self._dp = dp
        self._all_btns = dp.btns
        self._selected_btns = dp.selected_btns

        self._root = tk.Tk()

    def show_frame(self):
        # 初始化主面板

        main_frame = tk.Frame(self._root, width=500, height=500, background="white")
        main_frame.grid(padx=5, pady=5, row=0)  # , expand=True, fill=tk.BOTH

        # 添加button，100*100

        for i in range(self._dp.NUMBER_OF_ITEM):
            for j in range(self._dp.NUMBER_OF_ITEM):
                bt = tk.Button(main_frame, background=self._dp.DEFAULT_COLOR, bd=1, width=3, height=1)
                self._dp.btns[i][j] = iButton(bt, False, i, j)  # 将该按钮对象保存到数组中
                tmp = self._dp.btns[i][j]
                bt["command"] = (lambda x=bt, check_button=tmp: self._dp.onclick(check_button))
                bt.grid(row=i, column=j)

        button_label = tk.Label(self._root)
        button_label.grid(padx=5, pady=5, row=1)

        # 添加开始按钮
        btn_start = tk.Button(button_label, text="Start", command=self._dp.start)
        btn_start.pack(side=tk.LEFT, padx=5)

        # 添加暂停按钮
        btn_pause = tk.Button(button_label, text="Pause", command=self._dp.pause)
        btn_pause.pack(side=tk.LEFT, padx=5)

        # 添加导入地图按钮
        btn_load_map = tk.Button(button_label, text="Load_map", command=self.load_select_file)
        btn_load_map.pack(side=tk.LEFT, padx=5)

        # 添加导出地图按钮
        btn_save_map = tk.Button(button_label, text="Save_map", command=self.output_to_selected_file)
        btn_save_map.pack(side=tk.LEFT, padx=5)

        # 添加清空地图按钮
        btn_clear = tk.Button(button_label, text="Clear", command=self._dp.clear)
        btn_clear.pack(side=tk.LEFT, padx=5)

        # 添加下一步按钮
        btn_refresh = tk.Button(button_label, text="Next", command=self._dp.next_step)
        btn_refresh.pack(side=tk.LEFT, padx=5)

        # 添加换颜色按钮
        btn_change_color = tk.Button(button_label, text="Change_color", command=self._dp.change_color)
        btn_change_color.pack(side=tk.LEFT, padx=5)

        # 添加退出按钮
        btn_quit = tk.Button(button_label, text="Quit", command=self._exe_quit)
        btn_quit.pack(side=tk.LEFT, padx=5)

        self._root.mainloop()

    # 定义退出操作
    def _exe_quit(self):
        self._dp.quit()  # 首先调用Processer中的退出操作
        self._root.quit()

    # 定义选择加载地图操作
    def load_select_file(self):
        file = tkFileDialog.askopenfile(parent=self._root, title='选择文件')
        # 把得到的文件名全权交给Processer处理
        if file != None:
            self._dp.load_map(file)

    # 定义选择输出地图到文件操作
    def output_to_selected_file(self):
        file = tkFileDialog.asksaveasfile(parent=self._root, title='选择位置')
        # 把得到的文件名全权交给Processer处理
        if file != None:
            result = self._dp.output_map(file)
            print ("result=", result)


if __name__ == "__main__":
    from Processer import Processer

    processer = Processer()
    lf = LFrame(processer)
    lf.show_frame()
